package com.pdstudio.http;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.bean.County;
import com.pdstudio.bean.NewStationState;
import com.pdstudio.bean.ResponseData;
import com.pdstudio.bean.SimpleCity;
import com.pdstudio.bean.StationState;
import com.pdstudio.bean.WaterRiver;
import com.pdstudio.bean.WaterStation;
import com.pdstudio.bean.WaterStationSingle;
import android.content.Context;
import android.util.Log;

public class StationHttpMethod extends HttpMethodBase {

	private String funcName = "station";
	private String GETWEATHERFORECASTS = "getweatherforecasts";
	private String GETWATERSTATIONS = "getwaterstations";
	private String GETSTATIONSTATES = "getstationstatus";
	private String GETNEWSTATIONSTATES = "getstationlist";
	private String GETCITYS = "getcities";
	private String GETRIVERS = "getriverlist";
	private String GETWATERSTATIONSINGLE = "getwaterstationsingle";
	private static final String GETALLCOUNTIES = "getallcounty";
	private Context mContext;

	public StationHttpMethod(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
	}

	/*
	 * 获取河道列表
	 */
	public List<WaterRiver> GetRivers(String appKey, String addressCode) {
		List<WaterRiver> result = new ArrayList<WaterRiver>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("address", addressCode);
			String uri = buildUri(funcName, GETRIVERS, appKey, parameters);
			Log.v("uri", uri);
			ResponseData<List<WaterRiver>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<WaterRiver>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取所有县市
	 */
	public List<County> GetAllCounties(String appKey) {
		List<County> result = new ArrayList<County>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			String uri = buildUri(funcName, GETALLCOUNTIES, appKey, parameters);
			Log.v("uri", uri);
			ResponseData<List<County>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<County>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取水文站点列表
	 */
	public List<WaterStation> GetWaterStationsByRiverId(String appKey,
			String riverId) {
		List<WaterStation> result = new ArrayList<WaterStation>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("riverName", riverId);
			String uri = buildUri(funcName, GETWATERSTATIONS, appKey,
					parameters);
			Log.v("uri", uri);
			ResponseData<List<WaterStation>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<WaterStation>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取站点实时情况
	 */
	public List<StationState> GetStationStates(String appKey, String stationid,
			String stationname, String timestamp) {
		List<StationState> result = new ArrayList<StationState>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();

			parameters.put("stationid", stationid);
			parameters.put("stationname", stationname);
			parameters.put("timestamp", timestamp);
			String uri = buildUri(funcName, GETSTATIONSTATES, appKey,
					parameters);
			Log.v("uri", uri);
			ResponseData<List<StationState>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<StationState>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 新获取站点实时情况
	 */
	public List<NewStationState> GetNewStationStates(String appKey,
			String county, String startTime, String endTime) {
		List<NewStationState> result = new ArrayList<NewStationState>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			if (!startTime.equals("")) {
				Date date = sdf.parse(startTime);
				String startTime2 = sdf2.format(date);
				startTime = startTime2;
			}

			if (!endTime.equals("")) {
				Date date2 = sdf.parse(endTime);
				String endTime2 = sdf2.format(date2);
				endTime = endTime2;
			}

			parameters.put("county", county);
			parameters.put("startDate", startTime.replace(" ", "%20"));
			parameters.put("endDate", endTime.replace(" ", "%20"));
			String uri = buildUri(funcName, GETNEWSTATIONSTATES, appKey,
					parameters);
			Log.v("uri", uri);
			ResponseData<List<NewStationState>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<NewStationState>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取站点实时情况(List列表使用)
	 */
	public List<StationState> GetStationStatesList(String appKey,
			String stationid, String stationname, String timestamp, int flag) {
		List<StationState> result = new ArrayList<StationState>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("stationid", stationid);
			parameters.put("stationname", stationname);
			parameters.put("timestamp", timestamp);
			parameters.put("showflag", flag + "");
			String uri = buildUri(funcName, GETSTATIONSTATES, appKey,
					parameters);
			Log.v("uri", uri);
			ResponseData<List<StationState>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<StationState>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取单站水文信息
	 */
	public WaterStationSingle GetStationSingle(String appKey, String stationid) {
		WaterStationSingle result = new WaterStationSingle();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			parameters.put("id", stationid);
			// SimpleDateFormat sDateFormat = new
			// SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			// String date = sDateFormat.format(new java.util.Date());
			// parameters.put("timestamp", date);
			String uri = buildUri(funcName, GETWATERSTATIONSINGLE, appKey,
					parameters);
			Log.v("uri", uri);
			ResponseData<WaterStationSingle> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<WaterStationSingle>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 获取城市
	 */
	public List<SimpleCity> GetSimpleCitys(String appKey) {
		List<SimpleCity> result = new ArrayList<SimpleCity>();
		try {
			Map<String, String> parameters = new HashMap<String, String>();
			String uri = buildUri(funcName, GETCITYS, appKey, parameters);
			Log.v("uri", uri);
			ResponseData<List<SimpleCity>> response = new Gson().fromJson(
					connGetServerForResult(uri),
					new TypeToken<ResponseData<List<SimpleCity>>>() {
					}.getType());
			if (response != null && response.getCode().equals(HTTPSUCCESS)) {
				result = response.getData();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
